package com.rickpan.dto.contacts;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.rickpan.entity.FriendRequest;
import com.rickpan.entity.User;

import java.time.LocalDateTime;

/**
 * 好友申请响应DTO
 */
public class FriendRequestResponse {

    private Long id;
    private UserInfo fromUser;
    private UserInfo toUser;
    private FriendRequest.Status status;
    private String message;
    private String responseMessage;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime expiresAt;

    // 内部类：用户信息
    public static class UserInfo {
        private Long id;
        private String username;
        private String realName;
        private String avatarUrl;
        private User.UserType userType;

        public UserInfo() {}

        public UserInfo(User user) {
            this.id = user.getId();
            this.username = user.getUsername();
            this.realName = user.getRealName();
            this.avatarUrl = user.getAvatarUrl();
            this.userType = user.getUserType();
        }

        // Getter和Setter方法
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getRealName() { return realName; }
        public void setRealName(String realName) { this.realName = realName; }
        public String getAvatarUrl() { return avatarUrl; }
        public void setAvatarUrl(String avatarUrl) { this.avatarUrl = avatarUrl; }
        public User.UserType getUserType() { return userType; }
        public void setUserType(User.UserType userType) { this.userType = userType; }
    }

    // 构造函数
    public FriendRequestResponse() {}

    public FriendRequestResponse(FriendRequest request, User fromUser, User toUser) {
        this.id = request.getId();
        this.fromUser = new UserInfo(fromUser);
        this.toUser = new UserInfo(toUser);
        this.status = request.getStatus();
        this.message = request.getMessage();
        this.responseMessage = request.getResponseMessage();
        this.createdAt = request.getCreatedAt();
        this.updatedAt = request.getUpdatedAt();
        this.expiresAt = request.getExpiresAt();
    }

    // 静态工厂方法
    public static FriendRequestResponse from(FriendRequest request, User fromUser, User toUser) {
        return new FriendRequestResponse(request, fromUser, toUser);
    }

    // 业务方法
    public boolean isExpired() {
        return LocalDateTime.now().isAfter(expiresAt);
    }

    public boolean isPending() {
        return status == FriendRequest.Status.PENDING && !isExpired();
    }

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public UserInfo getFromUser() {
        return fromUser;
    }

    public void setFromUser(UserInfo fromUser) {
        this.fromUser = fromUser;
    }

    public UserInfo getToUser() {
        return toUser;
    }

    public void setToUser(UserInfo toUser) {
        this.toUser = toUser;
    }

    public FriendRequest.Status getStatus() {
        return status;
    }

    public void setStatus(FriendRequest.Status status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getResponseMessage() {
        return responseMessage;
    }

    public void setResponseMessage(String responseMessage) {
        this.responseMessage = responseMessage;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public LocalDateTime getExpiresAt() {
        return expiresAt;
    }

    public void setExpiresAt(LocalDateTime expiresAt) {
        this.expiresAt = expiresAt;
    }

    @Override
    public String toString() {
        return "FriendRequestResponse{" +
                "id=" + id +
                ", fromUser=" + fromUser +
                ", toUser=" + toUser +
                ", status=" + status +
                ", message='" + message + '\'' +
                ", responseMessage='" + responseMessage + '\'' +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                ", expiresAt=" + expiresAt +
                '}';
    }
}
